package x10.enums;

import java.util.EnumSet;
import java.util.HashMap;
import x10.utilities.BitSetUtils;

/**
 *  House - enumeration of house codes (A - P) along with utility methods
 *  to create the nibble values for powerline and radio frequency versions
 *  and static methods for creating a house object
 *
 *  @author Denny Sheirer
 *  @date   20110124
 */

public enum House {
    A(6,6),
    B(14,7),
    C(2,4),
    D(10,5),
    E(1,8),
    F(9,9),
    G(5,10),
    H(13,11),
    I(7,14),
    J(15,15),
    K(3,12),
    L(11,13),
    M(0,0),
    N(8,1),
    O(4,2),
    P(12,3);

    private final int powerline;
    private final int radioFrequency;

    //Lookups to convert from integer to House code
    private static final HashMap<Integer,House> powerlineLookup = new HashMap<Integer,House>();
    private static final HashMap<Integer,House> radioFrequencyLookup = new HashMap<Integer,House>();

    //Hidden class to populate the hashmaps
    static {
        for(House h : EnumSet.allOf(House.class)){
            powerlineLookup.put(h.powerline, h);
            radioFrequencyLookup.put(h.radioFrequency, h);
        }
    }

    /**
     * Constructor
     * @param powerline
     * @param radioFrequency
     */
    House(int powerline, int radioFrequency){
        this.powerline = powerline;
        this.radioFrequency = radioFrequency;
    }

    /**
     * Utility method to get byte value of powerline version of the house code
     * @return - byte powerline version
     */
    public byte getPowerLineVersion(){
        return (byte) powerline;
    }

    /**
     * Utility method to get byte value of radio frequency version of the house code
     * @return byte radio frequency version
     */
    public byte getRadioFrequencyVersion(){
        return (byte) radioFrequency;
    }

    /**
     * Method to get House code object from int value of powerline version byte
     * @param house
     * @return
     */
    public static House getHouseFromPowerline(int house){
        return powerlineLookup.get(house);
    }

    /**
     * Method to get House code object from int value of radio frequency version byte
     * @param house
     * @return
     */
    public static House getHouseFromRadioFrequency(int house){
        return radioFrequencyLookup.get(house);
    }

    /**
     * Method to get byte powerline version house code nibble in upper nibble position
     * @return - byte value of house code - shifted to upper nibble
     */
    public byte getPowerlineHouseCodeInUpperNibble(){
        return BitSetUtils.getNibble((byte)powerline, "LOWER", "UPPER");
    }

    /**
     * Method to get byte radio frequency version house code nibble in upper nibble position
     * @return byte value of house code - shifted to upper nibble
     */
    public byte getRadioFrequencyHouseCodeInUpperNibble(){
        return BitSetUtils.getNibble((byte)radioFrequency, "LOWER", "UPPER");
    }
}
